from pydantic import BaseModel, Field, validator
from typing import Optional, List, ForwardRef, Annotated
from datetime import datetime
from enum import IntEnum

# 添加性别枚举
class Gender(IntEnum):
    FEMALE = 0
    MALE = 1
    SECRET = 2

class UserBase(BaseModel):
    username: str
    gender: Gender = Gender.SECRET  # 使用枚举，默认保密
    bio: Optional[str] = None

    @validator('gender', pre=True)
    def convert_gender(cls, v):
        if isinstance(v, Gender):
            return v.value  # 转换枚举为整数
        return v

class User(UserBase):
    id: str
    profile_image: Optional[str]  # 改 avatar -> profile_image
    created_at: datetime
    avatar_id: Optional[str]  # 修改这里
    is_admin: int = 0

    class Config:
        from_attributes = True

class UserCreate(UserBase):
    password: str

class UserUpdate(BaseModel):
    gender: Optional[Gender] = None
    bio: Optional[str] = None

    @validator('gender', pre=True)
    def convert_gender(cls, v):
        if v is None:
            return v
        if isinstance(v, Gender):
            return v.value
        return v

class AvatarBase(BaseModel):
    prompt_settings: dict
    appearance_image: str
    voice_sample: str
    video_sample: Optional[str] = None

class AvatarCreate(AvatarBase):
    pass

class Avatar(AvatarBase):
    id: str
    user_id: str
    avatar_likes_count: int

    class Config:
        from_attributes = True

class AvatarUpdate(BaseModel):
    prompt_settings: Optional[dict] = None
    appearance_image: Optional[str] = None
    voice_sample: Optional[str] = None
    video_sample: Optional[str] = None

class CommentBase(BaseModel):
    content: str

class CommentCreate(CommentBase):
    pass  # 不需要 parent_id，因为它会从 URL 路径中获取

class Comment(CommentBase):
    id: str
    created_at: datetime
    author_id: str
    post_id: str
    parent_id: Optional[str]
    author: User
    replies: List['Comment'] = []

    class Config:
        from_attributes = True

class PostBase(BaseModel):
    title: str = Field(..., max_length=100, description="帖子标题")
    content: str = Field(..., description="帖子内容")

class PostCreate(PostBase):
    pass

class PostUpdate(BaseModel):
    title: Optional[str] = Field(None, max_length=100)
    content: Optional[str] = None

class PostLike(BaseModel):
    id: str
    post_id: str
    user_id: str
    created_at: datetime

    class Config:
        from_attributes = True

class Post(PostBase):
    id: str
    created_at: datetime
    author_id: str
    author: User
    comments: List[Comment] = []
    post_likes_count: int = 0  # 改名：likes_count -> post_likes_count

    class Config:
        from_attributes = True

class FriendRequestBase(BaseModel):
    to_user_id: str

class FriendRequestCreate(FriendRequestBase):
    pass

class FriendRequest(FriendRequestBase):
    friend_req_id: str
    from_user_id: str
    status: str
    created_at: datetime
    from_user: User
    to_user: User

    class Config:
        from_attributes = True

class ChatMessageBase(BaseModel):
    content: str

class FriendChatMessageCreate(ChatMessageBase):
    to_user_id: str

class FriendChatMessage(ChatMessageBase):
    id: str
    from_user_id: str
    to_user_id: str
    created_at: datetime
    is_read: bool
    from_user: User
    to_user: User

    class Config:
        from_attributes = True

class AvatarChatMessageCreate(ChatMessageBase):
    avatar_id: str

class AvatarChatMessage(ChatMessageBase):
    id: str
    user_id: str
    avatar_id: str
    created_at: datetime
    is_from_user: bool
    user: User
    avatar: Avatar

    class Config:
        from_attributes = True

class ChatSessionBase(BaseModel):
    pass

class FriendChatSession(ChatSessionBase):
    id: str
    user1_id: str
    user2_id: str
    created_at: datetime
    updated_at: datetime
    user1: User
    user2: User

    class Config:
        from_attributes = True

class AvatarChatSession(ChatSessionBase):
    id: str
    user_id: str
    avatar_id: str
    created_at: datetime
    updated_at: datetime
    user: User
    avatar: Avatar

    class Config:
        from_attributes = True

class ChatResponse(BaseModel):
    message: str 